package com.example.musicplayer.dialogTools;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.example.musicplayer.MusicScoreManager;
import com.example.musicplayer.R;
import com.example.musicplayer.musicClass.MusicInfo;

public class EditMusicScoreDialog extends BaseDialog {
    private final MusicScoreManager.MusicScoreInfo mMusicScoreInfo;
    private EditText mEditLyric;
    private EditText mEditMelody;
    private EditText mEditArrangement;
    private EditText mEditFavor;
    private TextView mDisplayFinal;
    private OnDataChangedListener mOnDataChangedListener;

    public interface OnDataChangedListener {
        void onDataChanged();
    }

    public EditMusicScoreDialog(Context context, MusicScoreManager.MusicScoreInfo musicScoreInfo) {
        super(context);
        mMusicScoreInfo = musicScoreInfo;
        if (mMusicScoreInfo != null) {
            init();
        }
    }

    @Override
    protected View setDialogView(Context context) {
        View view = View.inflate(context, R.layout.dialog_edit_music_score, null);
        mEditLyric = view.findViewById(R.id.edit_lyric);
        mEditMelody = view.findViewById(R.id.edit_melody);
        mEditArrangement = view.findViewById(R.id.edit_arrangement);
        mEditFavor = view.findViewById(R.id.edit_favor);
        mDisplayFinal = view.findViewById(R.id.display_final);
        view.findViewById(R.id.clear).setOnClickListener(v -> {
            String text = 0f + "";
            mEditLyric.setText(text);
            mEditMelody.setText(text);
            mEditArrangement.setText(text);
            mEditFavor.setText(text);
            mDisplayFinal.setText(text);
            if (mMusicScoreInfo != null) {
                mMusicScoreInfo.setLyric(0f);
                mMusicScoreInfo.setMelody(0f);
                mMusicScoreInfo.setArrangement(0f);
                mMusicScoreInfo.setFavor(0f);
                if (mOnDataChangedListener != null) {
                    mOnDataChangedListener.onDataChanged();
                }
            }
        });
        view.findViewById(R.id.confirm).setOnClickListener(v -> {
            //set new scores for music
            float scoreLyric = 0f;
            float scoreMelody = 0f;
            float scoreArrangement = 0f;
            float scoreFavor = 0f;
            try {
                scoreLyric = Float.parseFloat(mEditLyric.getText().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                scoreMelody = Float.parseFloat(mEditMelody.getText().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                scoreArrangement = Float.parseFloat(mEditArrangement.getText().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                scoreFavor = Float.parseFloat(mEditFavor.getText().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (mMusicScoreInfo != null) {
                mMusicScoreInfo.setLyric(scoreLyric);
                mMusicScoreInfo.setMelody(scoreMelody);
                mMusicScoreInfo.setArrangement(scoreArrangement);
                mMusicScoreInfo.setFavor(scoreFavor);
                String displayFinal = mMusicScoreInfo.getFinal() + "";
                mDisplayFinal.setText(displayFinal);
                if (mOnDataChangedListener != null) {
                    mOnDataChangedListener.onDataChanged();
                }
            }
        });
        return view;
    }

    @Override
    protected void setDialogLayout(Context context) {
        Window dialogWindow = mDialog.getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = (int) context.getResources().getDimension(R.dimen.dialog_edit_music_score_width);
            lp.height = (int) context.getResources().getDimension(R.dimen.dialog_edit_music_score_height);
            lp.gravity = Gravity.CENTER;
            dialogWindow.setAttributes(lp);
        }
    }

    @Override
    public void show() {
        super.show();
        mEditLyric.requestFocus();
    }

    private void init() {
        MusicInfo musicInfo = MusicInfo.getMusicInfoById(mMusicScoreInfo.getID());
        String text;
        if (musicInfo != null) {
            text = musicInfo.getTitle() + " - " + musicInfo.getArtist();
            ((TextView) mDialogView.findViewById(R.id.music_info)).setText(text);
        }
        text = mMusicScoreInfo.getLyric() + "";
        mEditLyric.setText(text);
        text = mMusicScoreInfo.getMelody() + "";
        mEditMelody.setText(text);
        text = mMusicScoreInfo.getArrangement() + "";
        mEditArrangement.setText(text);
        text = mMusicScoreInfo.getFavor() + "";
        mEditFavor.setText(text);
        text = mMusicScoreInfo.getFinal() + "";
        mDisplayFinal.setText(text);
    }

    public void setOnDataChangedListener(OnDataChangedListener listener) {
        mOnDataChangedListener = listener;
    }
}